package com.work.mdass.controller;

import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.work.mdass.domain.OrderApplyDetials;
import com.work.mdass.domain.Result;
import com.work.mdass.service.OrderApplyDetialsService;
import com.work.mdass.util.OrderApplyDetialsResultUtil;

@Controller
@RequestMapping("/admin/detials")
public class OrderApplyDetialsController {

	@Autowired
	private OrderApplyDetialsService orderApplyDetialsService;
	
	/*
	 * 新增一条详细信息
	 */
	@PostMapping("/save")
	@ResponseBody
	public Result<OrderApplyDetials> save(@Valid OrderApplyDetials orderApplyDetials){
		
		return OrderApplyDetialsResultUtil.success(orderApplyDetialsService.addOrderApplyDetials(orderApplyDetials));
		
	}
	/*
	 * 修改明细
	 */
	
	@PostMapping("/updata")
	@ResponseBody
	public Result<OrderApplyDetials> updata(@Valid OrderApplyDetials orderApplyDetials,BindingResult bindingResult){
		if(bindingResult.hasErrors()) {
			return OrderApplyDetialsResultUtil.error(1, bindingResult.getFieldError().getDefaultMessage());
		}
		return OrderApplyDetialsResultUtil.success(orderApplyDetialsService.updata(orderApplyDetials));
	}
	/*
	 * 通过申请编号获得明细
	 */
	@PostMapping("/bySerial")
	@ResponseBody
	public Result<OrderApplyDetials> getByApllySerial(@RequestParam(required = false) Integer applySerial){
		
		return OrderApplyDetialsResultUtil.success(orderApplyDetialsService.getDetials(applySerial));
	}
	/*
	 * 通过编号获取明细
	 */
	@PostMapping("/byId/{id}")
	@ResponseBody
	public Result<OrderApplyDetials> getById(@PathVariable("id") Integer id){
		return OrderApplyDetialsResultUtil.success(orderApplyDetialsService.getById(id));
	}
	/*
	 * 通过课程名称查找
	 */
	@PostMapping("/byCourseName")
	@ResponseBody
	public Result<OrderApplyDetials> getByCourseName(@RequestParam(required = false) String courseName){
		
		return OrderApplyDetialsResultUtil.success(orderApplyDetialsService.getByCourseName(courseName));
	}
	/*
	 * 删除明细
	 */
	@PostMapping("/delete")
	@ResponseBody
	public Result<OrderApplyDetials> delete(@RequestParam(required = false) Integer id){
		
		return OrderApplyDetialsResultUtil.success(orderApplyDetialsService.deleteDetials(id));
	}
	/*
	 * 通过教师名称获得明细
	 */
	@PostMapping("/byTName")
	@ResponseBody
	public Map<Object,Object> getByTName(@RequestParam(required = false) String tName){
		return orderApplyDetialsService.getByTName(tName);
	}
	/*
	 * 通过关键字匹配课程名称
	 */
	@PostMapping("/byKeyWord")
	@ResponseBody
	public Map<Object,Object> getByKeyWord(@RequestParam(required = false) String keyWord){
		return orderApplyDetialsService.getByKeyWord(keyWord);
	}
	/*
	 * 用户个人数据统计
	 */
	@PostMapping("/userAnalysis")
	@ResponseBody
	public Map<Object,Object> userAnalysis(@RequestParam(required = false) String tName,
			@RequestParam(required = false) String keyWord){
		return orderApplyDetialsService.userAnalysis(tName, keyWord);
	}
}
